<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CORS 测试页面</title>
    <style>
        body {
            font-family: 'Microsoft YaHei', sans-serif;
            max-width: 800px;
            margin: 50px auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background-color: white;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        h1 {
            color: #333;
            text-align: center;
        }
        button {
            background-color: #4CAF50;
            color: white;
            padding: 12px 24px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
            margin: 10px 5px;
        }
        button:hover {
            background-color: #45a049;
        }
        .result {
            margin-top: 20px;
            padding: 15px;
            border-radius: 5px;
            white-space: pre-wrap;
            word-wrap: break-word;
        }
        .success {
            background-color: #d4edda;
            border: 1px solid #c3e6cb;
            color: #155724;
        }
        .error {
            background-color: #f8d7da;
            border: 1px solid #f5c6cb;
            color: #721c24;
        }
        .info {
            background-color: #d1ecf1;
            border: 1px solid #bee5eb;
            color: #0c5460;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>Kong 网关 CORS 测试</h1>
        
        <div>
            <button onclick="testTraceApiWrite()">测试 traceapiwrite</button>
            <button onclick="testTraceApiRead()">测试 traceapiread</button>
            <button onclick="testMateApiWrite()">测试 mateapiwrite</button>
        </div>
        
        <div id="result" class="result info" style="display:none;"></div>
    </div>

    <script>
        function showResult(message, type = 'info') {
            const resultDiv = document.getElementById('result');
            resultDiv.textContent = message;
            resultDiv.className = 'result ' + type;
            resultDiv.style.display = 'block';
        }

        async function testTraceApiWrite() {
            showResult('正在测试 traceapiwrite...', 'info');
            try {
                const response = await fetch('http://115.190.179.32:8000/traceapiwrite/api/Order/Update', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({
                        id: 1,
                        orderNumber: 'TEST-001'
                    })
                });
                
                showResult(
                    `✅ CORS 测试成功！\n\n` +
                    `状态码: ${response.status}\n` +
                    `状态文本: ${response.statusText}\n\n` +
                    `说明: 预检请求（OPTIONS）已通过，没有 CORS 错误。`,
                    'success'
                );
            } catch (error) {
                showResult(
                    `❌ CORS 测试失败！\n\n` +
                    `错误信息: ${error.message}\n\n` +
                    `说明: 如果看到 CORS 相关错误，说明配置还有问题。`,
                    'error'
                );
            }
        }

        async function testTraceApiRead() {
            showResult('正在测试 traceapiread...', 'info');
            try {
                const response = await fetch('http://115.190.179.32:8000/traceapiread/api/Order/GetOrderList?pageIndex=1&pageSize=10', {
                    method: 'GET',
                    headers: {
                        'Content-Type': 'application/json',
                    }
                });
                
                showResult(
                    `✅ CORS 测试成功！\n\n` +
                    `状态码: ${response.status}\n` +
                    `状态文本: ${response.statusText}\n\n` +
                    `说明: 请求成功，没有 CORS 错误。`,
                    'success'
                );
            } catch (error) {
                showResult(
                    `❌ CORS 测试失败！\n\n` +
                    `错误信息: ${error.message}\n\n` +
                    `说明: 如果看到 CORS 相关错误，说明配置还有问题。`,
                    'error'
                );
            }
        }

        async function testMateApiWrite() {
            showResult('正在测试 mateapiwrite...', 'info');
            try {
                const response = await fetch('http://115.190.179.32:8000/mateapiwrite/api/MaterialWithdrawal/AddMaterialWithdrawal', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({
                        materialName: 'TEST'
                    })
                });
                
                showResult(
                    `✅ CORS 测试成功！\n\n` +
                    `状态码: ${response.status}\n` +
                    `状态文本: ${response.statusText}\n\n` +
                    `说明: 预检请求（OPTIONS）已通过，没有 CORS 错误。`,
                    'success'
                );
            } catch (error) {
                showResult(
                    `❌ CORS 测试失败！\n\n` +
                    `错误信息: ${error.message}\n\n` +
                    `说明: 如果看到 CORS 相关错误，说明配置还有问题。`,
                    'error'
                );
            }
        }

        // 页面加载时显示当前地址
        window.onload = function() {
            showResult(
                `当前页面地址: ${window.location.origin}\n\n` +
                `请点击上面的按钮测试 CORS 配置是否正常。\n\n` +
                `如果测试成功，说明 Kong 网关的 CORS 配置已经正确。`,
                'info'
            );
        };
    </script>
</body>
</html>


